--
-- Author: ping.wang
-- Date: 2018-08-01 14:38:15
--
local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PlayerExpTipsNode,true))
    if node == nil then
        display.doLayout(sprite)
    end
    sprite:setNodeEventEnabled(true)
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize(self._ui.Panel:getContentSize())

    self._arrow = self._ui.arrow
    self._text = self._ui.tlt

	self._arrowWidth = self._arrow:getContentSize().width

    self._expBarWidth = display.ui_width --经验条的总长度
    local timeline = self:getTimeline()
    if timeline then
    	self._timeline = timeline
    	timeline:pause()
	end
end

function ClassRef:setExpBarWidth(len)
	self._expBarWidth = len
end

function ClassRef:playAction(name, isLoop)
	if self._timeline then
		if name then
    		self._timeline:play(name, isLoop)
    	else
    		self._timeline:gotoFrameAndPlay(0, isLoop)
    	end
	end
end

function ClassRef:setTips(stepDatas)
	local addExp = 0
	local tmpExp = stepDatas[1].exp
	local tmpLv = stepDatas[1].level
	for i = 2, #stepDatas do
		if stepDatas[i].level ~= tmpLv then
			addExp = addExp + stepDatas[i].exp
		else
			addExp = addExp + stepDatas[i].exp - tmpExp
		end
		tmpExp = stepDatas[i].exp
		tmpLv = stepDatas[i].level
	end

	local lastStep = stepDatas[#stepDatas]
	local percent = lastStep.exp/lastStep.maxExp
	self._text:setString(string.format(L("gotexp"),addExp))
	local curPosWidth = self._expBarWidth * percent --计算出当前显示的位置
	local wd = self._ui.Panel:getContentSize().width
	if (curPosWidth + wd * 0.5) > self._expBarWidth then
		self:setPositionX(self._expBarWidth - wd * 0.5)
		local arrowPos = wd + curPosWidth - self._expBarWidth
		if arrowPos >= (wd - self._arrowWidth) then
			arrowPos = wd - self._arrowWidth
		end

		self._arrow:setPositionX(arrowPos)
	elseif (curPosWidth - wd * 0.5) < 0 then
		self:setPositionX(wd * 0.5)
		local arrowPos = curPosWidth
		if arrowPos < self._arrowWidth then
			arrowPos = self._arrowWidth
		end
		self._arrow:setPositionX(arrowPos)
	else
		self:setPositionX(curPosWidth)
		self._arrow:setPositionX(0.5 * wd)
	end

	--准备执行一个显示后隐藏的动画
	local act = cca.seq({cca.callFunc(function()
        self:setVisible(true)
        self:playAction("in", false)
        end),cca.delay(2), cca.callFunc(function()
        self:playAction("out", false)
        end),cca.delay(0.5),cca.hide()})
	act:setTag(10)
	self:stopActionByTag(10)
    self:runAction(act)
end

return ClassRef